package com.loocup.demo;

import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author loocup
 * @description
 * @since 2019/1/15
 */
public class MybatisGenerater {


    @Test
    public void jufanDb() throws ClassNotFoundException {
        String driverClass = "com.mysql.jdbc.Driver";
        String basePackage = "com.jufan";
        Class.forName(driverClass);
        generateByJava("src/main/java", basePackage, true, true, driverClass,
                "jdbc:mysql://192.168.21.22:3306/jufan?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false",
                "root", "We-are-hero-2015", new String[]{"biz_contract"}, "entity", "mapper", "dao");
    }

    private void generateByJava(String targetProject, String basePackage, boolean overWriteXml, boolean overWriteJava, String driverClass, String oneUrl,
                                String oneUsername, String onePassword,
                                String[] tableList,
                                String entity, String mapper, String dao) {
        String targetPackage = basePackage + "." + entity;
        String targetPackage1 = basePackage + "." + mapper;
        String targetPackage2 = basePackage + "." + dao;
        try {
            List<String> warnings = new ArrayList<>();
            Configuration config = new Configuration();
            Context context = new Context(ModelType.CONDITIONAL);
            context.setId("DB2Tables");
            context.setTargetRuntime("MyBatis3");
            // commont
            CommentGeneratorConfiguration commentGenerator = new CommentGeneratorConfiguration();
            commentGenerator.addProperty("suppressAllComments", "true");
            commentGenerator.addProperty("suppressDate", "true");
            context.setCommentGeneratorConfiguration(commentGenerator);
            // db
            JDBCConnectionConfiguration jdbcConnection = new JDBCConnectionConfiguration();
            jdbcConnection.setConnectionURL(oneUrl);
            jdbcConnection.setDriverClass(driverClass);
            jdbcConnection.setUserId(oneUsername);
            jdbcConnection.setPassword(onePassword);
            context.setJdbcConnectionConfiguration(jdbcConnection);
            // java type
            JavaTypeResolverConfiguration javaTypeResolver = new JavaTypeResolverConfiguration();
            javaTypeResolver.addProperty("forceBigDecimal", Boolean.TRUE.toString());
            context.setJavaTypeResolverConfiguration(javaTypeResolver);

            // java model generator
            JavaModelGeneratorConfiguration javaModelGenerator = new JavaModelGeneratorConfiguration();
            javaModelGenerator.setTargetPackage(targetPackage);
            javaModelGenerator.setTargetProject(targetProject);
            javaModelGenerator.addProperty("trimStrings", Boolean.TRUE.toString());
            context.setJavaModelGeneratorConfiguration(javaModelGenerator);

            // sql map xml
            SqlMapGeneratorConfiguration sqlMapGenerator = new SqlMapGeneratorConfiguration();
            sqlMapGenerator.setTargetPackage(targetPackage1);
            sqlMapGenerator.setTargetProject(targetProject);
            sqlMapGenerator.addProperty("enableSubPackages", Boolean.TRUE.toString());

            context.setSqlMapGeneratorConfiguration(sqlMapGenerator);

            // java client generator
            JavaClientGeneratorConfiguration javaClientGenerator = new JavaClientGeneratorConfiguration();
            javaClientGenerator.addProperty("enableSubPackages", Boolean.TRUE.toString());
            javaClientGenerator.setConfigurationType("XMLMAPPER");
            javaClientGenerator.setTargetPackage(targetPackage2);
            javaClientGenerator.setTargetProject(targetProject);
            context.setJavaClientGeneratorConfiguration(javaClientGenerator);

            // tables % means all
            if (tableList == null) {
                TableConfiguration tc = new TableConfiguration(context);
                tc.setTableName("%");
                tc.setCountByExampleStatementEnabled(false);
                tc.setUpdateByExampleStatementEnabled(false);
                tc.setDeleteByExampleStatementEnabled(false);
                tc.setSelectByExampleStatementEnabled(false);
                tc.setSelectByPrimaryKeyStatementEnabled(true);
                context.addTableConfiguration(tc);
            } else {
                for (String s : tableList) {
                    TableConfiguration tc = new TableConfiguration(context);
                    tc.setTableName(s);
                    tc.setCountByExampleStatementEnabled(false);
                    tc.setUpdateByExampleStatementEnabled(false);
                    tc.setDeleteByExampleStatementEnabled(false);
                    tc.setSelectByExampleStatementEnabled(false);
                    tc.setSelectByPrimaryKeyStatementEnabled(true);
                    context.addTableConfiguration(tc);
                }
            }

            // 覆盖xml生成
            if (overWriteXml) {
                PluginConfiguration pluginConfig = new PluginConfiguration();
                pluginConfig.setConfigurationType(UnmergeableXmlMappersPlugin.class.getTypeName());
                context.addPluginConfiguration(pluginConfig);
            }
            // add context
            config.addContext(context);
            DefaultShellCallback callback = new DefaultShellCallback(overWriteJava);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
